package com.cloud.shopping.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.shopping.goods.mapper.BrandMapper;
import com.cloud.shopping.goods.mapper.CategoryMapper;
import com.cloud.shopping.goods.mapper.SkuMapper;
import com.cloud.shopping.goods.mapper.SpuMapper;
import com.cloud.shopping.goods.model.*;
import com.cloud.shopping.goods.service.SpuService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author John.zhang
 * @since 2020-12-28
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {

    @Resource
    private SpuMapper spuMapper;

    @Resource
    private SkuMapper skuMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private BrandMapper brandMapper;


    @Override
    public void saveProduct(Product product) {
        //1.保存Spu
        Spu spu = product.getSpu();

        if (StringUtils.isEmpty(spu.getId())) {
            //已上架
            spu.setIsMarketable(1);
            //未删除
            spu.setIsDelete(0);
            //审核已通过
            spu.setStatus(1);
            spuMapper.insert(spu);
        } else {
            //修改
            spuMapper.updateById(spu);
            //删除Sku集合
            skuMapper.delete(new QueryWrapper<Sku>().eq("spu_id", spu.getId()));
        }

        //2.保存List<Sku>
        Date date = new Date();
        Category category = categoryMapper.selectById(spu.getCategoryThreeId());
        Brand brand = brandMapper.selectById(spu.getBrandId());
        for (Sku sku : product.getSkuas()) {
            //SKU名称
            // {"适合人群":"有一定java基础的人","书籍分类":"科技"}
            StringBuilder name = new StringBuilder(spu.getName());
            Map<String, String> skuAttrMap = JSON.parseObject(sku.getSkuAttribute(), Map.class);
            for (Map.Entry<String, String> entry : skuAttrMap.entrySet()) {
                name.append("  ").append(entry.getValue());
            }
            sku.setName(name.toString());
            //创建时间
            sku.setCreateTime(date);
            //修改时间
            sku.setUpdateTime(date);
            //分类ID
            sku.setCategoryId(spu.getCategoryThreeId());
            //分类名字
            sku.setBrandName(brand.getName());
            //品牌ID
            sku.setBrandId(spu.getBrandId());
            //品牌名字
            sku.setCategoryName(category.getName());
            //spuId
            sku.setSpuId(spu.getId());
            //状态 商品状态 1-正常，2-下架，3-删除
            sku.setStatus(1);

            //添加
            skuMapper.insert(sku);
        }
    }

    @Override
    public Product findBySupId(String id) {
        //查询Spu
        Spu spu = spuMapper.selectById(id);
        //查询Sku集合
        QueryWrapper<Sku> queryWrapper = new QueryWrapper<Sku>();
        queryWrapper.eq("spu_id", id);
        List<Sku> skuas = skuMapper.selectList(queryWrapper);

        //产品详情信息
        Product product = new Product();
        product.setSpu(spu);
        product.setSkuas(skuas);
        return product;
    }
}
